import Vue from 'vue'
import Router from 'vue-router'


      
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import {checkUserLoggedIn}from '@/views/login/index'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */



export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard',hideSidebar: true }
    }]
  },
  {
    path: '/Technology',
    component: Layout,
    redirect: '/Technology/DownTime',
    name: 'Technology',
    meta: {
      title: '技术运营联动', 
    },
    children: [
      {
        path: 'DownTime',
        component: () => import('@/views/Technology/DownTime/Index'), // Parent router-view
        name: 'DownTime',
        redirect: '/Technology/DownTime/MessageSynchronous',
        meta: { title: '停机维护', icon: 'field-time-outlined' },
        children: [
          {
            path: 'MessageSynchronous',
            component: () => import('@/views/Technology/DownTime/MessageSynchronous'),
            name: 'MessageSynchronous',
            meta: { title: '停机维护信息同步' }
          },
          {
            path: 'MessageSynchronousDetail/:id',
            component: () => import('@/views/Technology/DownTime/MessageSynDetail'),
            name: 'MessageSynchronousDetail',
            meta: { title: '停机维护信息同步详情' },
            hidden: true
          },
          {
            path: 'MessageInquire',
            component: () => import('@/views/Technology/DownTime/MessageInquire'),
            name: 'MessageInquire',
            meta: { title: '停机维护信息查询' }
          },

        ]
      },
      {
        path: 'EventDispose',
        component: () => import('@/views/Technology/EventDispose/Index'),
        meta: { title: '事件处理', icon: 'snippets-outlined' },
        redirect: '/Technology/EventDispose/EventProcess',
        name: "EventDispose",
        children: [
          {
            path: 'EventProcess',
            name: 'EventProcess',
            meta: { title: '事件处理' },
            component: () => import('@/views/Technology/EventDispose/EventProcess')
          },
          {
            path: 'EventNotice',
            name: 'EventNotice',
            meta: { title: '事件辅助通知' },
            component: () => import('@/views/Technology/EventDispose/EventNotice')
          },

        ]
      },
      {
        path: 'Product',
        name: "Product",
        component: () => import('@/views/Technology/Product'),
        meta: { title: '生产压制', icon: 'safari' }
      },
      {
        path: 'Danger',
        name: "Danger",
        component: () => import('@/views/Technology/Danger'),
        meta: { title: '风险排查', icon: 'focus-3-line' }
      },
      {
        path: 'InterConnection',
        name: "InterConnection",
        component: () => import('@/views/Technology/InterConnection'),
        meta: { title: '互联互通', icon: 'a-ControlPanel' }
      }
    ]
  },
  {
    path: '/Serve',
    component: Layout,
    redirect: '/Serve/FindTradeView',
    meta: { title: '运营服务联动' },
    children: [
      {
        path: 'FindTradeView',
        name: "FindTradeView",
        component: () => import('@/views/Serve/FindTrade/FindTradeView'),
        meta: { title: '交易协查', icon: 'view-outlined' },

      },
      {
        path: 'ProtectContacts',
        name: "ProtectContacts",
        component: () => import('@/views/Serve/ProtectContacts'),
        meta: { title: '通讯录维护', icon: 'contact' }
      },

    ]
  },

  {
    path: '/Other',
    component: Layout,
    redirect: '/Other/WorkUrging',
    name: 'Other',
    meta: { title: '其他' },
    children: [
      {
        path: 'WorkUrging',
        name: 'WorkUrging',
        component: () => import('@/views/Other/WorkUrging/Index'),
        meta: { title: '工作催办', icon: 'mail-outlined' },
      },
      {
        path: 'MonitorTransmissions',
        name: 'MonitorTransmissions',
        component: () => import('@/views/Other/MonitorTransmissions/Index'),
        meta: { title: '监控传输', icon: 'shipinjiankong' }
      }
    ]
  },
  {
    path: '/SystemManage',
    component: Layout,
    redirect: '/SystemManage/MenuManage',
    name: 'SystemManage',
    meta: { title: '系统管理' },
    children: [
      {
        path: 'MenuManage',
        name: 'MenuManage',
        component: () => import('@/views/SystemManage/MenuManage/MenuList'),
        meta: { title: '菜单管理', icon: 'menu-mtest' },
      },
      {
        path: 'RoleManage',
        name: 'RoleManage',
        component: () => import('@/views/SystemManage/RoleManage/RoleList'),
        meta: { title: '角色管理', icon: 'role-m' }
      },
      {
        path: 'UserManage',
        name: 'UserManage',
        component: () => import('@/views/SystemManage/UserManage/UserList'),
        meta: { title: '用户管理', icon: 'user-m' }
      },
      {
        path: 'SystemLog',
        name: 'SystemLog',
        component: () => import('@/views/SystemManage/SystemLog/LogList'),
        meta: { title: '系统错误日志', icon: 'log-m' }
      }
    ]
  },




  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]



const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()



// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    if (!checkUserLoggedIn()) {
      // 用户未登录，重定向到登录页
      next('/login');
    } else {
      // 用户已登录，允许访问受保护的路由
      next();
    }
  } else {
    // 对于不需要登录的路由，允许访问
    next();
  }
})
export default router
